package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class EmployeeDao {
	private DBHelp<Employee> db = new DBHelp<Employee>();
	
	private class EmployeeMap implements RowMapper<Employee>{

		@Override
		public Employee mapperRow(ResultSet rs) throws SQLException {
			Employee employee = new Employee();
			employee.setId(rs.getInt("id"));
			employee.setName(rs.getString("name"));
			employee.setPassword(rs.getString("password"));
			employee.setPic(rs.getString("pic"));
			
			return employee;
		}
	}

	public Employee findByNameAndPassword(String name, String password) {
		String sql = "SELECT id, name, password, pic FROM t_employee WHERE name=? AND password=?";
		return db.executeQueryForObject(sql, new EmployeeMap(), name, password);
	}

	//插入新员工
	public void addEmp(String name, String pwd) {
		String sql = "INSERT INTO t_employee(name,password) VALUES(?,?)";
		db.executeSQL(sql,name, pwd);
	}
	//修改密码
	public void update(Employee em) {
	String sql = "UPDATE t_employee SET password = ? where id = ?";
	db.executeSQL(sql, em.getPassword(),em.getId());
		
	}
	//名字不是tom的员工的信息查询出来
	public List<Employee> findAll() {
		String sql = "select id, name, password, pic from t_employee WHERE name <> ?";
		return db.executeQueryForList(sql, new EmployeeMap(), "tom");
	}

	public List<Employee> finAllMemberByProId(int proId) {
		String sql = "SELECT id, `name`, `password`, pic FROM t_employee WHERE id IN (SELECT t_employee_id FROM t_project_employee WHERE t_project_id = ? AND role= 'member')";
		return db.executeQueryForList(sql, new EmployeeMap(), proId);
	}

	public void uploadHead(int id, String pic) {
		String sql = "UPDATE t_employee SET pic = ? where id = ?";
		db.executeSQL(sql, pic,id);
		
	}

	public Employee findByName(String name) {
		String sql = "SELECT id, name, password, pic FROM t_employee WHERE name=?";
		return db.executeQueryForObject(sql, new EmployeeMap(), name);
	}
}
